﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;

namespace MySite.Timer
{
    public abstract class ITimer
    {
        //请求的最小时间间隔
        public int minMinute;

        //请求的最大时间间隔
        public int maxMinute;

        //默认的请求间隔时间
        public static readonly int defaultMinute = 30 * 60 * 1000;

        //当前的请求间隔
        public int currentMinute = defaultMinute;

        //延迟的时间
        public int dueTime = 1000;

        //定时器
        public System.Threading.Timer GlobalTimer;

        public ITimer()
        {
            dueTime = 1000;
            Init();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            GlobalTimer = new System.Threading.Timer(new TimerCallback(GlobalTimer_ToDo), null, Timeout.Infinite, Timeout.Infinite);
        }

        /// <summary>
        /// 比较已经存在的信息的md5值与获取到的md5值,然后按照规则计算当前的请求间隔
        /// </summary>
        public virtual void GlobalTimer_ToDo(object state)
        {
        
        }


        public void Start()
        {
            GlobalTimer.Change(dueTime, currentMinute);
        }

        //
        // 摘要:
        //     更改计时器的启动时间和方法调用之间的间隔，用 64 位有符号整数度量时间间隔。
        //
        // 参数:
        //   dueTime:
        //     在调用构造 System.Threading.Timer 时指定的回调方法之前的延迟时间量（以毫秒为单位）。指定 System.Threading.Timeout.Infinite
        //     可防止重新启动计时器。指定零 (0) 可立即重新启动计时器。
        //
        //   period:
        //     调用构造 System.Threading.Timer 时指定的回调方法的时间间隔（以毫秒为单位）。指定 System.Threading.Timeout.Infinite
        //     可以禁用定期终止。
        //
        public  void Start(int dt, int period)
        {
            GlobalTimer.Change(dt, period);
        }

        //停止定时器
        public  void Stop()
        {
            GlobalTimer.Change(Timeout.Infinite, Timeout.Infinite);
        }

        /// <summary>
        /// BinarySerialize
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public string BinarySerialize(object o)
        {
            string result = null;
            if (o != null)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(stream, o);
                    stream.Position = 0;
                    result = Convert.ToBase64String(stream.ToArray());
                }
            }
            return result;
        }

        /// <summary>
        /// BinaryDeserialize
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="hash"></param>
        /// <returns></returns>
        public T BinaryDeserialize<T>(string hash)
        {
            T result = default(T);
            if (!string.IsNullOrEmpty(hash))
            {
                using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(hash)))
                {
                    stream.Position = 0;
                    BinaryFormatter formatter = new BinaryFormatter();
                    result = (T)formatter.Deserialize(stream);
                }
            }
            return result;
        }
    }
}
